package com.hu.zhcc.shiro.service.xy.service.impl;


import com.hu.zhcc.shiro.dao.xy.TAssessAnswerMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessMapper;
import com.hu.zhcc.shiro.dao.xy.TAssessQuestionOptionMapper;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssess;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessAnswer;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.service.xy.service.TAssessAnswerService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TAssessAnswerImpl implements TAssessAnswerService {
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TAssessMapper tAssessMapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;

    @Override
    public int addAnswer(List<TAssessQuestion> tAssessQuestions, HttpSession session) {
        TMember user = (TMember) session.getAttribute("user");
        Integer markTotal=0;
        for(int i=0;i<tAssessQuestions.size();i++ ){
            TAssessQuestion tAssessQuestion = tAssessQuestions.get(i);
            TAssessAnswer tAssessAnswer = new TAssessAnswer();
            //设置问题id
            tAssessAnswer.setQuestionId(tAssessQuestion.getId());
            //设置答案id
            if(tAssessQuestion.getIsRadio().equals("0")){
                tAssessAnswer.setAnswerId(tAssessQuestion.getSelectOption()+"");
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark();
            }else{
                String options="";
                for(int j=0;j<tAssessQuestion.getCheckOptions().size();j++){
                    options+=tAssessQuestion.getCheckOptions().get(j);
                    Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(j).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                    if(j<tAssessQuestion.getCheckOptions().size()-1){
                        options+=",";
                    }
                }
                tAssessAnswer.setAnswerId(options);
            }
            //设置当前登录人id
            tAssessAnswer.setUserId(user.getId());
            tAssessAnswerMapper.insertSelective(tAssessAnswer);
        }
        TAssess tAssess = new TAssess();
        tAssess.setMemberId(user.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tAssess.setTime(simpleDateFormat.format(new Date()));
        tAssess.setState(0);
        tAssessMapper.insertSelective(tAssess);
//        System.out.println(tAssess);

        return 0;
    }

    @Override
    public List<TAssessAnswer> selectTAssessByUserId(Integer userId, Integer questionId) {
        return tAssessAnswerMapper.selectTAssessByUserId(userId ,questionId);
    }


}
